from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.script import Manager
from info import db, create_app, models
from info.models import User

app = create_app('development')

manager = Manager(app)
Migrate(app, db)
manager.add_command('db', MigrateCommand)


@manager.option('-n', '-name', dest="name")
@manager.option('-p', '-password', dest="password")
def createsuperuser(name, password):
    """
    命令行添加管理员
    :param name:
    :param password:
    :return:
    """
    if not all([name, password]):
        print("参数不全")
        return

    user = User()
    user.password = password
    user.is_admin = True
    user.nick_name = name
    user.mobile = name
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        print(e)
        print("添加失败")
    print("添加成功")


if __name__ == '__main__':
    print(app.url_map)
    manager.run()
